﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace VerificadorF3101
{
    public partial class frmMain : Form
    {
        delegate void SetTextCallback(string text);
        delegate void SetProgressCallback(int progress);

        public frmMain()
        {
            InitializeComponent();

            Carpeta = Properties.Settings.Default.Carpeta;
            this.txtCarpeta.Text = Carpeta;
        }

        public string Carpeta { get; set; }

        private void btnExaminar_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog
            {
                SelectedPath = Carpeta,
                ShowNewFolderButton = false,
                Description = "Carpeta de Despachos"
            };

            if (dialog.ShowDialog() == DialogResult.Cancel)
                return;

            Carpeta = dialog.SelectedPath;

            this.txtCarpeta.Text = Carpeta;
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            var dialog = MessageBox.Show(
                "¿Confirma que desea salir?",
                "Salir",
                MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button2);

            if (dialog != DialogResult.Yes)
                e.Cancel = true;
        }

        private void btnIniciar_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(Carpeta))
            {
                MessageBox.Show("Seleccione una carpeta", 
                    this.Text,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);

                return;
            }

            if (!Directory.Exists(Carpeta))
            {
                MessageBox.Show("No existe el directorio '" + Carpeta + "'",
                    this.Text,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);

                return;
            }

            this.btnCerrar.Enabled = false;
            this.btnIniciar.Enabled = false;
            this.btnExaminar.Enabled = false;

            worker.WorkerReportsProgress = true;

            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

            worker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(worker_ProgressChanged);

            worker.StatusReported += new BGStatusReportedEventHandler(worker_StatusReported);

            worker.RunWorkerAsync(Carpeta);

            return;

        }

        void worker_StatusReported(object sender, StatusEventArgs e)
        {
            string text = e.Status;

            if (this.richTextBox1.InvokeRequired)
            {
                // It's on a different thread, so use Invoke.
                SetTextCallback d = new SetTextCallback(SetText);

                this.Invoke
                    (d, new object[] { text });
            }
            else
            {
                // It's on the same thread, no need for Invoke
                richTextBox1.AppendText(text);
                richTextBox1.AppendText("\n");
            }


        }

        private void SetText(string text)
        {
            richTextBox1.AppendText(text);
            richTextBox1.AppendText("\n");
        }

        private void SetProgress(int progress)
        {
            barProgreso.Value = progress;
            barProgreso.Refresh();
        }

        private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            int progress = e.ProgressPercentage;

            if (this.barProgreso.InvokeRequired)
            {
                // It's on a different thread, so use Invoke.
                SetProgressCallback d = new SetProgressCallback(SetProgress);

                this.Invoke
                    (d, new object[] { progress });
            }
            else
            {
                // It's on the same thread, no need for Invoke
                this.barProgreso.Value = progress;
            }
        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Proceso Finalizado.\n\nVerifique los resultados dentro de la carpeta seleccionada.",
                this.Text,
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);

            this.btnCerrar.Enabled = true;
            this.btnIniciar.Enabled = true;
            this.btnExaminar.Enabled = true;
        }

        private void verificador_ProgressChanged(object sender, ProgressEventArgs e)
        {
            worker.ReportProgress(e.Progress);
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            string carpeta = e.Argument as string;

            Verificador verificador = new Verificador(carpeta);

            verificador.ProgressChanged += new ProgressChangedEventHandler(verificador_ProgressChanged);

            verificador.StatusReported += new StatusReportedEventHandler(verificador_StatusReported);

            if (verificador != null)
            {
                verificador.Verificar();
            }
        }

        void verificador_StatusReported(object sender, StatusEventArgs e)
        {
            worker.ReportStatus(e.Status);
        }
    }
}
